package com.dream.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dream.bean.Employee;
import com.dream.bean.Msg;
import com.dream.service.EmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
public class EmployeeController {

	@Autowired
	private EmployeeService employeeService;
	
	/**
	 * 得到员工分页数据
	 * @param pageNum 当前页
	 * @param pageSize 页码大小
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/employee",method=RequestMethod.GET)
	public Msg employeeList(@RequestParam(value="pageNum",defaultValue="1")Integer pageNum,
			@RequestParam(value="pageSize",defaultValue="5")Integer pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		List<Employee> employees=employeeService.getEmployeeAll();
		PageInfo pageInfo=new PageInfo(employees, 5);
		return Msg.Success().putData("pageInfo", pageInfo);
	}

	/**
	 * 添加员工
	 * @param employee
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/employee",method=RequestMethod.POST)
	public Msg employeeAdd(@Valid Employee employee,BindingResult bindingResult) {
		Map<String, String> errorFieldsMap=new HashMap<String, String>();
		if (bindingResult.hasErrors()) {
			List<FieldError> fieldErrors = bindingResult.getFieldErrors();
			for (int i = 0; i < fieldErrors.size(); i++) {
				errorFieldsMap.put(fieldErrors.get(i).getField(), fieldErrors.get(i).getDefaultMessage());
			}
		}
		boolean available=employeeService.checkEmployeeName(employee.getEmpName());
		if (!available) {
			errorFieldsMap.put("empName", "该员工已存在");
		}
		if (errorFieldsMap.size()>0) {
			return Msg.Fail().putData("errorFieldsMap", errorFieldsMap);
		}
		int result=employeeService.employeeAdd(employee);
		if (result>0) {
			return Msg.Success();
		}
		return Msg.Fail();
	}
	
	@ResponseBody
	@RequestMapping("/checkUser")
	public Msg checkEmployeeName(@RequestParam("employeeName")String employeeName) {
		boolean available=employeeService.checkEmployeeName(employeeName);
		return available?Msg.Success():Msg.Fail();
	}
	
	@ResponseBody
	@RequestMapping(value="/employee/{empId}",method=RequestMethod.PUT)
	public Msg employeeUpdate(@Valid Employee employee,BindingResult bindingResult) {
		Map<String, String> errorFieldsMap=new HashMap<String, String>();
		if (bindingResult.hasErrors()) {
			List<FieldError> fieldErrors = bindingResult.getFieldErrors();
			for (int i = 0; i < fieldErrors.size(); i++) {
				errorFieldsMap.put(fieldErrors.get(i).getField(), fieldErrors.get(i).getDefaultMessage());
			}
		}
		if (errorFieldsMap.size()>0) {
			return Msg.Fail().putData("errorFieldsMap", errorFieldsMap);
		}
		boolean result=employeeService.employeeUpdate(employee);
		if (result) {
			return Msg.Success();
		}
		return Msg.Fail();
	}
	
	@ResponseBody
	@RequestMapping(value="/employee/{ids}",method=RequestMethod.DELETE)
	public Msg employeeDel(@PathVariable("ids")String ids) {
		List<Integer> list=new ArrayList<Integer>();
		if (ids.contains("-")) {
			String[] split = ids.split("-");
			for (int i = 0; i < split.length; i++) {
				list.add(Integer.parseInt(split[i]));
			}
		}else{
			list.add(Integer.parseInt(ids));
		}
		boolean success=employeeService.employeeDel(list);
		return success?Msg.Success():Msg.Fail();
	}
	
	@ResponseBody
	@RequestMapping(value="/employee/{id}",method=RequestMethod.GET)
	public Msg getEmployee(@PathVariable("id")Integer id) {
		Employee employee=employeeService.getEmployee(id);
		if (employee==null) {
			return Msg.Fail();
		}
		return Msg.Success().putData("employee", employee);
	}
	
	
}
